package com.vishnu.main;

import java.util.ArrayList;
import java.util.Iterator;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.vishnu.pojo.Employee;
import com.vishnu.pojo.Product;

public class PracticeOnHQLQueries {
public static void main(String[] args) {
	Configuration cfg=new Configuration();
	cfg.configure("hib.cfg.xml");
	SessionFactory sf=cfg.buildSessionFactory();
	Session hsession=sf.openSession();
	Transaction tx=hsession.beginTransaction();
	//using hqlQuery
//	String hqlQuery="from Employee where employeeSalary>5000";
	//using positional parameters in hqlQuery
//	String hqlQuery="from Employee e where e.employeeSalary>? and e.employeeSalary<?";
	//using named Positional parameters in hqlQuery 
	
//	String hqlQuery ="from Employee e where e.employeeSalary>=:minValue and e.employeeSalary<=:maxValue";
	//using order clause in hqlQuery
	String hqlQuery ="from Employee e where e.employeeSalary>=:minValue and e.employeeSalary<=:maxValue order by e.employeeSalary asc";
	
	Query query=hsession.createQuery(hqlQuery);
	query.setDouble("minValue", 4000);
	query.setDouble("maxValue", 100000);
	
/*	ArrayList<Employee> list=(ArrayList<Employee>)query.list();
	Iterator<Employee> it=list.iterator();
	while(it.hasNext()){
		Employee employee=(Employee)it.next();
		System.out.println("Product Id="+employee.getEmployeeId());
		System.out.println("Product Name="+employee.getEmployeeName());
		System.out.println("Product Salary="+employee.getEmployeeSalary());
		
	}*/
	
	//retriving in another way
	
	ArrayList list=(ArrayList)query.list();
	
	Iterator it=list.iterator();
	while(it.hasNext()){
		Object o=it.next();
			
			System.out.println("object="+o);
			Employee employee=(Employee)o;
		System.out.println("Product Id="+employee.getEmployeeId());
		System.out.println("Product Name="+employee.getEmployeeName());
		System.out.println("Product Salary="+employee.getEmployeeSalary());
		
	}
	
	tx.commit();
	hsession.close();
	sf.close();
}
}
